/*
 * Copyright (c) 2012 Czech Technical University in Prague.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */
package cz.cvut.kbss.owldiff.view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import cz.cvut.kbss.owldiff.diff.OWLDiffConfiguration;
import org.semanticweb.owlapi.model.OWLAxiom;

import cz.cvut.kbss.owldiff.syntax.DLSyntax;
import cz.cvut.kbss.owldiff.syntax.Syntax;

/**
 * @author fritzek
 */
public class MergeAcceptDialog extends javax.swing.JDialog {

    private static final long serialVersionUID = -804134291384623521L;
    private SimpleModel deleteModel;
    private SimpleModel addModel;
    boolean returnValue = false;
    private File ontologyPath;
    private Syntax syntax;
    private boolean mergeToFile;

    public MergeAcceptDialog(final Syntax s, java.awt.Frame parent,
                             boolean modal, Collection<OWLAxiom> toAdd,
                             Collection<OWLAxiom> toDelete, File ontologyPath,
                             boolean mergeToFile) {
        super(parent, true);
        this.mergeToFile = mergeToFile;
        this.syntax = s;
        addModel = new SimpleModel(toAdd);
        deleteModel = new SimpleModel(toDelete);
        this.initComponents();
        this.ontologyPath = ontologyPath;
        this.btnFile.setText(ontologyPath.toString());
        this.pack();
        this.setVisible(true);
    }

    private class SimpleModel extends AbstractListModel {

        private static final long serialVersionUID = -8773675784198384667L;
        private final List<OWLAxiom> owl;

        public SimpleModel(Collection<OWLAxiom> s) {
            owl = new ArrayList<OWLAxiom>();
            if (s != null) {
                owl.addAll(s);
            }
        }

        public int getSize() {
            return owl.size();
        }

        public Object getElementAt(int i) {
            return syntax.writeAxiom(owl.get(i), false, null, false);
        }
    }

    public File getSelectedFile() {
        return ontologyPath;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed"
    // desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane2 = new javax.swing.JSplitPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        btnOK = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        btnFile = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jSplitPane2.setResizeWeight(0.5);
        jSplitPane2.setLastDividerLocation(-1);
        jSplitPane2.setPreferredSize(new java.awt.Dimension(600, 133));

        jList2.setBackground(new java.awt.Color(217, 253, 137));
        jList2.setModel(this.addModel);
        jList2.setPreferredSize(null);
        jScrollPane2.setViewportView(jList2);

        jSplitPane2.setLeftComponent(jScrollPane2);

        jList1.setBackground(new java.awt.Color(254, 204, 194));
        jList1.setModel(this.deleteModel);
        jList1.setPreferredSize(null);
        jScrollPane1.setViewportView(jList1);

        jSplitPane2.setRightComponent(jScrollPane1);

        btnOK.setText(OWLDiffConfiguration.getCoreTranslations().getString("core.merge-dialog.ok"));
        btnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });

        btnCancel.setText(OWLDiffConfiguration.getCoreTranslations().getString("core.merge-dialog.cancel"));
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        jLabel2.setText(OWLDiffConfiguration.getCoreTranslations().getString("core.merge-dialog.to-delete"));

        jLabel3.setText(OWLDiffConfiguration.getCoreTranslations().getString("core.merge-dialog.to-add"));

        jLabel4.setText(OWLDiffConfiguration.getCoreTranslations().getString("core.merge-dialog.merging-to"));

        btnFile.setText(OWLDiffConfiguration.getCoreTranslations().getString("core.merge-dialog.ontology-location"));
        btnFile.setContentAreaFilled(false);
        btnFile.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btnFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFileActionPerformed(evt);
            }
        });

        final GridBagLayout gb = new GridBagLayout();
        getContentPane().setLayout(gb);

        final JPanel pnlTop = new JPanel(new GridBagLayout());
        final GridBagConstraints gbc2 = new GridBagConstraints();
        gbc2.weightx = 1;
        gbc2.anchor = GridBagConstraints.LINE_START;
        pnlTop.add(jLabel3, gbc2);
        gbc2.anchor = GridBagConstraints.LINE_END;
        pnlTop.add(jLabel2, gbc2);

        final JPanel pnlBottom = new JPanel(new GridBagLayout());
        final GridBagConstraints gbc3 = new GridBagConstraints();
        gbc3.fill = GridBagConstraints.HORIZONTAL;
        gbc3.insets = new Insets(3, 3, 3, 3);
        if (mergeToFile) {
            pnlBottom.add(jLabel4, gbc3);
            gbc3.weightx = 1;
            pnlBottom.add(btnFile, gbc3);
            gbc3.weightx = 0;
        } else {
            pnlBottom.add(new JLabel(), gbc3);
            gbc3.weightx = 1;
            pnlBottom.add(new JLabel(), gbc3);
            gbc3.weightx = 0;
        }
        pnlBottom.add(btnOK, gbc3);
        pnlBottom.add(btnCancel, gbc3);

        final GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(3, 3, 3, 3);
        gbc.weightx = 1;
        gbc.gridx = 0;
        gbc.gridy = 0;
        getContentPane().add(pnlTop, gbc);

        gbc.weighty = 1;
        gbc.gridy++;
        getContentPane().add(jSplitPane2, gbc);

        gbc.weighty = 0;
        gbc.gridy++;
        getContentPane().add(pnlBottom, gbc);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {// GEN-
        // FIRST
        // :
        // event_jButton1ActionPerformed
        this.returnValue = true;
        dispose();
    }// GEN-LAST:event_jButton1ActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {// GEN-
        // FIRST
        // :
        // event_jButton2ActionPerformed
        this.returnValue = false;
        dispose();
    }// GEN-LAST:event_jButton2ActionPerformed

    private void btnFileActionPerformed(java.awt.event.ActionEvent evt) {// GEN-
        // FIRST
        // :
        // event_jButton3ActionPerformed
        JFileChooser fc = new JFileChooser();
        fc.setDialogTitle(OWLDiffConfiguration.getCoreTranslations().getString("core.merge-dialog.select-output-file"));
        fc.setCurrentDirectory(ontologyPath);
        if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            ontologyPath = fc.getSelectedFile();
        }
        this.btnFile.setText(ontologyPath.toString());
    }// GEN-LAST:event_jButton3ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnFile;
    private javax.swing.JButton btnOK;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSplitPane jSplitPane2;

    // End of variables declaration//GEN-END:variables

    public static void main(String[] args) {
        new MergeAcceptDialog(new DLSyntax(), new JFrame(), false,
                new ArrayList<OWLAxiom>(), new ArrayList<OWLAxiom>(), new File(
                ""), false).setVisible(true);
    }
}
